package com.yanqu.road.server.cmd.patrons;

import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_PATRONS_SKILL_UPGRADE, desc = "门客技能升级")
public class PatronsSkillUpgradeCmd extends AbstractCommand {

	private int patronsId;

	private int skillId;

	private int type;

	private int goodsIndex;

	private int times;
	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PatronsProto.PatronsSkillUpgradeReqMsg reqMsg = PatronsProto.PatronsSkillUpgradeReqMsg.parseFrom(packet.getBytes());
		patronsId = reqMsg.getPatronsId();
		skillId = reqMsg.getSkillId();
		type = reqMsg.getType();
		if (reqMsg.hasIndex()) {
			goodsIndex = reqMsg.getIndex();
		}
		if (reqMsg.hasTimes()) {
			times = reqMsg.getTimes();
		} else {
			times = 1;
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ret = player.getModule(PatronsModule.class).skillUpgrade(patronsId, skillId, type, goodsIndex, times);
	}

	@Override
	public void sendData(GamePlayer player) {
		PatronsProto.PatronsSkillUpgradeRespMsg.Builder respMsg = PatronsProto.PatronsSkillUpgradeRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			respMsg.setPatronsId(patronsId);
			respMsg.setSkillId(skillId);
			respMsg.setType(type);
			UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
			UserPatronsSkill userPatronsSkill = userPatrons.getPatronsSkill(type, skillId);
			respMsg.setSkillLv(userPatronsSkill.getSkillLv());
		}
		player.sendPacket(Protocol.U_PATRONS_SKILL_UPGRADE, respMsg);
	}

}
